package com.szm.productserver.webui;

import com.szm.productserver.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
//@RefreshScope
public class ProductController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/getproduct")
    public Product getPorudct(){
        Product product=new Product();
        product.setProductId(48656);
        product.setProductName("碧根果225g");
        product.setServerAddress(getLocalInstanceInfo());
        return product;
    }

    private String getLocalInstanceInfo() {
        ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
        return serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort();
    }
}
